package com.suda.catalina.myapplication;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    public final static String REMOVE_ACTION = "com.suda.catalina.Remove";
    private FragmentManager fragmentManager;
    private Fragment fragment;
    private RadioGroup radioGroup;

    private IntentFilter intentFilter;
    private MyReceiver myReceiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //fragmenthe和radioGroup实现Tab选项卡
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        fragment = new InfoFragment();
        transaction.replace(R.id.content, fragment);
        transaction.commit();
        radioGroup = (RadioGroup) findViewById(R.id.rg_tab);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                switch (group.getCheckedRadioButtonId()) {
                    case R.id.tab_info:
                        fragment = new InfoFragment();
                        break;
                    case R.id.tab_person:
                        fragment = new PersonFragment();
                        break;
                    case R.id.tab_look:
                        fragment = new LookFragment();
                        break;
                    case R.id.tab_setting:
                        fragment = new SettingFragment();
                        break;
                    default:
                        break;
                }
                fragmentTransaction.replace(R.id.content, fragment);
                fragmentTransaction.commit();
            }
        });

        //动态broadcastReceiver监听网络变化
        intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        myReceiver = new MyReceiver();
        registerReceiver(myReceiver, intentFilter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_item:
                startActivity(new Intent(MainActivity.this, AddActivity.class));
                break;
            case R.id.remove_item:
                startActivity(new Intent(MainActivity.REMOVE_ACTION));
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
